﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using LuxuryResort.Entity;

namespace LuxuryResort.DAO
{
    public class RoomDAO : BaseDAO
    {
        public Room GetRoom(Guid id, Guid userid, Guid resortId)
        {
            Room room = null;
            List<LRMPConnection> cs = GetConnectionString(userid, resortId);
            if (cs.Count > 0)
            {
                using (var context = new LRMPEntities(cs[0].ConnectionString))
                {
                    room = context.Rooms.FirstOrDefault(r => r.RMID == id);
                }
            }

            return room;
        }

        public List<Room> GetRooms(Guid userid, Guid resortId)
        {
            var rooms = new List<Room>();
            List<LRMPConnection> cs = GetConnectionString(userid, resortId);
            if (cs.Count > 0)
            {
                using (var context = new LRMPEntities(cs[0].ConnectionString))
                {
                    rooms.AddRange(context.Rooms.Where(r => r.IsAvailable == true).ToList());
                }
            }

            return rooms;
        }

        public List<Room> GetAvailableRooms(Guid userId, Guid resortId, DateTime checkin, DateTime checkout,
            string roomType)
        {
            var rooms = new List<Room>();
            List<LRMPConnection> cs = GetConnectionString(userId, resortId);
            if (cs.Count > 0)
            {
                using (var context = new LRMPEntities(cs[0].ConnectionString))
                {
                    rooms.AddRange(
                        context.ExecuteStoreQuery<Room>("exec USP_Select_AvaiableRooms @checkin, @checkout, @roomType",
                            new SqlParameter("checkin", checkin),
                            new SqlParameter("checkout", checkout),
                            new SqlParameter("roomType", roomType)));
                }
            }

            return rooms;
        }
    }
}